package com.fit.pages.contactPerson;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.dao.contactPerson.ContactPersonDAO;
import com.fit.entities.ContactPerson;
import com.fit.pages.Confirmation;

public class Edit {

	@Property
	@Persist
	private ContactPerson contactPerson;
	@Inject
	private ContactPersonDAO contatcPersonDAO;
	@InjectPage
	private AddContactPerson addContactPerson;
	@InjectPage
	private Confirmation confirmation;
	
	
	Object onActivate(Long id) {
		contactPerson = contatcPersonDAO.findByID(id);
		if (contactPerson == null) {
			confirmation.setError("Greska prilikom prikaza zapisa korisnika! ");
			return confirmation;
		}
		return null;
	}

	@CommitAfter
	Object onSuccess() {
		try {
			contatcPersonDAO.update(contactPerson);
			addContactPerson.setMsg("Contact Person succesifuly changed!");
			return addContactPerson;
		} catch (Exception e) {
			confirmation.setError("Greska prilikom brisanja zapisa korisnika! ");
			return confirmation;
		}

	}
	
	
}
